<template>
  <div class="articles-add">
    <el-dialog :title="`${data.id ? '修改' : '添加'}文章`" :visible.sync="showAddDialog">
        <el-form ref="form" :model="dialogForm" :rules="rules">
            <el-form-item label="文章标题" label-width="80px" prop="title">
                <el-input v-model="dialogForm.title" placeholder="请输入文章标题"></el-input>
            </el-form-item>
            <el-form-item label="文章内容" label-width="80px" prop="articleBody">
                <quill-editor
                  v-model="dialogForm.articleBody"
                  :options="editorOptions">
                </quill-editor>
            </el-form-item>
            <el-form-item label="视频地址" label-width="80px" prop="videoUrl">
                <el-input v-model="dialogForm.videoURL" placeholder="请输入视频地址"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="showAddDialog = false">取消</el-button>
            <el-button @click="handleConfirm" type="primary">确认</el-button>
        </template>
    </el-dialog>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { add, update } from '@/api/hmmm/articles'

export default {
  name: 'ArticlesAdd',
  components: {
    quillEditor
  },
  props: {
    data: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      // 1. 对话框的显示与隐藏
      showAddDialog: false,
      // 2. 富文本编辑器
      editorOptions: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      },
      // 3. 对话框表单数据
      dialogForm: {
        id: null,
        title: null,
        articleBody: null,
        videoURL: null
      },
      // 4. 对话框表单校验规则
      rules: {
        title: [
          { required: true, message: '文章标题不能为空', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '文章内容不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 1. 显示添加对话框
    openDialog () {
      this.showAddDialog = true
      if (this.data.id) {
        const { id, title, articleBody, videoURL } = this.data
        this.dialogForm = { id, title, articleBody, videoURL }
      } else {
        this.dialogForm = {
          id: null,
          title: null,
          articleBody: null,
          videoURL: null
        }
      }
      // 清除校验效果
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
    // 2. 处理对话框确认按钮
    async handleConfirm () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (!this.data.id) {
            // 2.1 发送新增请求
            await add(this.dialogForm)
          } else { // 修改
            this.dialogForm.id = this.data.id
            await update(this.dialogForm)
          }
        }
        // 提示用户
        this.$message.success('操作成功')
        // 关闭对话框
        this.showAddDialog = false
        // 重新获取列表
        this.$emit('updateList')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.articles-add ::v-deep .ql-editor{
  height: 200px;
}
.articles-add ::v-deep .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
